package laboratorios.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ListaParesServlet
 */
@WebServlet("/ListaParesServlet")
public class ListaParesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListaParesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		String x = request.getParameter("txtNumInicio");
		String y = request.getParameter("txtNumFin");
		
		if (!(x).matches("([0-9]|)+")) { 
			//INVOCAR EN MEMORIA
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request,response);
			System.out.println("paso 1");
		} 
			

		if (!(y).matches("([0-9]|)+")) { 
			//INVOCAR EN MEMORIA
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request,response);
			System.out.println("paso 2" + y);
		} 	
				
		int i = Integer.parseInt(x);
		int j = Integer.parseInt(y);
		int t;
		
		if (i >= j  || i < 0 || j< 0) {
			//INVOCAR EN MEMORIA
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request,response);
			System.out.println("paso 2" + y);
		}
		
		int residuo;
		int par;
		String MSG="";
		residuo = 0;
		par = 2;
		t = 0;
		
		for (t = i; t<=j; t++){
		  residuo = t%par;
		  if (residuo == 0) {
				MSG = MSG + ", " + t + "";
		  }
		}

		//GUARDAR EN MEMORIA
		request.setAttribute("MENSAJE", MSG);
		
		//INVOCAR EN MEMORIA
		RequestDispatcher rd = request.getRequestDispatcher("CalcularPares.jsp");
		rd.forward(request,response);

	
	
	}

}
